Components.utils.import("resource://lucidity/lucidapi.jsm");

var myObserver =
{
    startup: function()
    {
        this.prefs = lucidapi.config.getLucidPrefBranch();
        this.prefs.QueryInterface(Components.interfaces.nsIPrefBranch2);
        this.prefs.addObserver("", this, false);
        this.refreshInformation();
    },

    observe: function( subject, topic, data )
    {
        if (topic != "nsPref:changed")
        {
            return;
        }
        switch (data) {
            case "keystore_path":
            case "hide_expired":
            case "enable_keysync":
                this.refreshInformation();
                break;
        }
    },

    shutdown: function()
    {
        this.prefs.removeObserver("", this);
    },

    refreshInformation: function()
    {
        var keypathtxtbox = document.getElementById("keystore-path-textbox");
        keypathtxtbox.value = lucidapi.config.keystore_path;
        updateKeyMenulist(document.getElementById("defkey-menulist"));
        updateGenericBox(document.getElementById('pubkeylist'),false);
        updateGenericBox(document.getElementById('seckeylist'),true);
    }
}


function chooseKeystorePath(reset)
{
    var keyringFolder = null;
    if (reset)
    {
        keyringFolder = "";
    }
    else
    {
        keyringFolder = lucidapi.openFileSelector(window, true, document.getElementById("keystore-path-textbox").value);
    }

    if (keyringFolder != null)
    {
        var preference = document.getElementById("keystore-path-pref");
        preference.value = keyringFolder;
    }
}

function doKeygen()
{
    lucidapi.openKeygenWizard();
    myObserver.refreshInformation();
}

function onKeytreeContextCommand(event)
{
    var cmd = event.target.id;
    var parnode = event.target.parentNode;
    var keytype = parnode.getAttribute("keytype");
    var keyid = parnode.getAttribute("keyid");
    switch (cmd) {
        case "keytreemenu-export":
            lucidapi.exportKeys(window,(keytype.indexOf("sec") == 0),keyid);
            break;
    }
}

function handleKeytreeClick(event){
    if (event.button < 2)
        return;
    var row = {}, column = {}, part = {};
    var tree = event.currentTarget;
    var treemenu = document.getElementById('keytreemenu');

    var boxobject = tree.boxObject;
    boxobject.QueryInterface(Components.interfaces.nsITreeBoxObject);
    boxobject.getCellAt(event.clientX, event.clientY, row, column, part);

    var treecolel = document.getElementById(tree.parentNode.id + "-" + "keyid");
//    lucidapi.error("treecolel: %s", Boolean(treecolel));
    var primecol = tree.columns.getColumnFor(treecolel);
    if (row.value > -1)
    {
        var label = tree.view.getCellText(row.value,primecol);
//        lucidapi.error("keyid: %s", label);
        treemenu.setAttribute("keytype",tree.parentNode.id);
        treemenu.setAttribute("keyid",label);
        treemenu.openPopup(null,"overlap",event.clientX,event.clientY,true,false);
    }
}

function onLoad( /* win */ )
{
    myObserver.startup();
    document.getElementById('pubkeylist').getTree().addEventListener("click", handleKeytreeClick, false);
    document.getElementById('seckeylist').getTree().addEventListener("click", handleKeytreeClick, false);
}

function onUnload( /* win */ )
{
    myObserver.shutdown();
}
